<template>
  <base-breadcrumb>
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <a-card :bordered="false">
      <!-- <div class="table-operator">
        <div class="pub-table-action-btn">
          <a-button
            v-btnPermission="'mm_028_add_01'"
            type="primary"
            icon="plus"
            @click="handleEdit(null,1)"
          >
            新增
          </a-button>


          <excel
            ref="exportSelect"
            file-name="供应商新增"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportSelect"
            >
              导出选中
            </a-button>
          </excel>
          <excel
            ref="exportAll"
            file-name="供应商新增"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportAll"
            >
              导出全部
            </a-button>
          </excel>
        </div>
        <div class="pub-table-select-count">
          <a-icon
            type="info-circle"
            theme="twoTone"
          />
          已选择{{ selectedRowKeys.length }}项
        </div>
      </div> -->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :start-index="0"
        :out-selected-row-keys="selectedRowKeys"
        :export-permission="{ select: '', all: '' }"
        :export-config="{
          use: false,
          select: $route.meta.title,
          all: $route.meta.title
        }"
      >
<!--        v-btnPermission="'mm_028_add_01'" -->
        <a-button slot="prefix" type="primary" v-if="hideBuquBtn()" icon="plus" @click="handleEdit(null, 1)"> 新增 </a-button>
        <a-upload v-if="hideBuquBtn()" :show-upload-list="false" :custom-request="importFunc" slot="prefix" accept=".csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
          <a-button>导入</a-button>
        </a-upload>
        <template
          slot="index"
          slot-scope="text, record, index"
        >
          {{ index + 1 }}
        </template>
        <template
          slot="auditStatusStr"
          slot-scope="text, record"
        >
          <list-status
            :status="record.auditStatus"
            :text="text"
          />
        </template>
        <div
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
            <a @click="handleEdit(record, 3)">查看</a>
            <template v-if="(record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED')&&hideBuquBtn()">
              <a @click="() => handleEdit(record, 2)">编辑</a>
              <a @click="() => handleDel(record.id)">删除</a>
            </template>
          </MoreButton>
        </div>
      </list-table>
      <!-- <pagination
        v-model="pageNo"
        :total="totalSize"
        :page-size.sync="pageSize"
        @change="handlePageChange"
        @showSizeChange="showSizeChange"
      /> -->
    </a-card>
  </base-breadcrumb>
</template>
<script>
import { supplierList, supplierListDel, uploadSupplier } from '@/api/material/supplier/supplier'
import { selectOption } from '@/api/material/goods/goods'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'

export default {
  name: 'a' + Date.now(),
  data: function () {
    return {
      loading: false,
      queryParam: {},
      pageNo: 1,
      pageSize: 10,
      totalSize: 0,
      searchArr: [
        // 搜索框
        // {
        //     name: '所属分公司',
        //     valueKey: 'branchCompanyId',
        //     type: SelectSubCompany
        // },
        // {
        //     name: '项目名称',
        //     type: SelectProject,
        //     valueKey: 'projectId'
        // },
        {
          name: '供应商编码',
          value: 'code',
          type: 'input'
        },
        {
          name: '供应商名称',
          value: 'name',
          type: 'input'
        },
        {
          name: '供应商考察编码',
          value: 'supplierSurveyCode',
          type: 'input'
        },
        {
          name: '创建人',
          value: 'creatorName',
          type: 'input'
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'timeRangeFormat'
        },
        {
          name: '状态',
          value: 'auditStatus',
          type: 'select',
          data: [
            { label: '草稿', value: 'DRAFT', enabled: true },
            { label: '已提交', value: 'PAST', enabled: true }
          ]
        }
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          scopedSlots: { customRender: 'index' },
          width: 70
        },
        {
          title: '供应商编码',
          dataIndex: 'code',
          width: 120
        },
        {
          title: '供应商名称',
          dataIndex: 'name',
          width: 250
        },
        {
          title: '供应商考察编码',
          dataIndex: 'supplierSurveyCode',
          width: 140
        },
        {
          title: '适用税率%',
          dataIndex: 'taxRateType',
          width: 120
        },
        {
          title: '创建人',
          dataIndex: 'creatorName'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 160
        },
        {
          title: '状态',
          width: this.STATUS_WIDTH,
          dataIndex: 'auditStatusStr',
          scopedSlots: { customRender: 'auditStatusStr' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          fixed: 'right',
          width: 150,
          scopedSlots: { customRender: 'operation' }
        }
      ],
      selectedRowKeys: [],
      selectId: [],
      selectedRows: [],
      chooseData: 0,
      // tableData: [],
      loadData: (params) => {
        let searchData = Object.assign(params, this.queryParam)
        return supplierList(searchData).then((res) => {
          return res
        })
      },
      pathName: this.$route.path
    }
  },
  created() {
    // this.list()
  },
  mounted: function () {
    this.$refs.table.refresh()
    // this.list()
    // this.$refs.exportSelect.tableToExcelFile(this.$refs.table)
    //   this.$refs.exportAll.tableToExcelFile(this.$refs.table)
    // console.log('mounted-----------table的Path', this.pathName)
    // 监听详情页返回刷新列表
    //this.pathName取的值是this.$route.path
    this.$bus.$on('handleRefresh' + this.pathName, () => {
      // console.log('--------------------收到了，刷新刷新刷新----------------',this.pathName)
      // this.list()
      this.$refs.table.refresh()
    })
  },
  beforeDestroy() {
    console.log('beforeDestroy-------table的Path', this.pathName)
    this.$bus.$off('handleRefresh' + this.pathName)
  },
  methods: {
    exportSelect: function () {
      if (this.selectedRowKeys.length > 0) {
        this.$refs.exportSelect.setDataAndGenerate(this.selectedRows)
      } else {
        this.$message.warning('请选择需要导出的数据')
      }
    },
    exportAll: function () {
      // 参数：查询条件、导出的回调
      this.pageNo = 1
      this.list(false, this.$refs.exportAll.setDataAndGenerate)
    },
    list(params, callback) {
      this.loading = true
      let _this = this
      let searchData = Object.assign(
        {
          pageSize: callback ? _this.totalSize : _this.pageSize,
          pageNo: _this.pageNo
        },
        params,
        this.queryParam
      )
      // if (searchData.createTime) {
      //     searchData.createTimeStart = _this.formatDate(searchData.createTime[0])
      //     searchData.createTimeEnd = _this.formatDate(searchData.createTime[1])
      // }
      searchData.supplierTypeEnum = 'SUPPLIER_ADD'

      supplierList(searchData).then((res) => {
        if (res.code === 200) {
          if (callback) {
            if (res.data.length) {
              callback(res.data)
            } else {
              this.$message.warning('没有可导出的数据')
            }
            this.loading = false
            return
          }
          _this.tableData = res.data
          _this.totalSize = res.totalSize
        }
        this.loading = false
      })
    },
    search(val) {
      this.pageNo = 1
      this.queryParam = val
      // this.list(val)
      this.$refs.table.refresh()
    },
    // handlePageChange(pageNo, pageSize) {
    //     this.pageNo = pageNo
    //     this.list()
    // },
    // showSizeChange(pageNo, pageSize) {
    //     this.pageNo = 1
    //     this.pageSize = pageSize
    //     this.list()
    // },
    // onSelectChange(selectedRowKeys, selectedRows) {
    //     let _this = this
    //     _this.selectId = []
    //     selectedRows.map(item => {
    //         _this.selectId.push(item.id)
    //     })
    //     this.chooseData = selectedRows.length
    //     _this.selectedRowKeys = selectedRowKeys
    //     _this.selectedRows = selectedRows
    // },
    handleDel(id) {
      let _this = this
      let params = id ? [id] : _this.selectId
      _this.delList(
        supplierListDel,
        () => {
          _this.search()
          this.selectedId = []
          this.selectedRowKeys = []
          this.selectedRows = []
          this.editingKey = ''
        },
        { ids: params }
      )
    },
    handleEdit(record, state) {
      this.$router.push({
        path: '/material/supplier/new/add',
        query: {
          row: record,
          state: state
        }
      })
    },
    async importFunc(data) {
      console.log(data.file)
      let fb = new FormData()
      fb.append('uploadFile ', data.file)
      let res = await uploadSupplier(fb)
      if (res.code === 200) {
        this.$noticeSuccess('导入成功')
      }
    }
  }
}
</script>
